var wxCharts = require('./../../utils/wxcharts-min');
var app = getApp();
var pieChart = null;
var lineChart = null;
var startPos = null;
var network = require("../../utils/network/network.js")
var api = require("../../utils/network/config.js").api;
Page({
  data: {
    stickPieDataList: [],
    stickLineData: {
      dates: [],
      stickNums: []
    }
  },
  loadData() {
    // let that = this
    // network.fetch({
    //     url: api.data_info,
    //     inBody: false,
    //     data: {
    //       //userId: app.globalData.userInfo.uid
    //       userId: getApp().globalData.userInfo.uid
    //     },
    //     method: 'GET',
    //   })
    //   .then(res => {
    //     console.log(res)
    //     that.setData({
    //       stickPieDataList: res.data.stickPieDataList,
    //       stickLineData: res.data.stickLineData
    //     })
    //   })

    return new Promise((resolve, reject) => {
      let that = this
      network.fetch({
          url: api.data_info,
          inBody: false,
          data: {
            userId: 22
            //userId: getApp().globalData.userInfo.uid
          },
          method: 'GET',
        })
        .then(res => {
          console.log(res)
          resolve(res.data)
          that.renderChart(res.data)
        })
    })

  },
  touchHandler: function (e) {
    lineChart.scrollStart(e);
  },
  moveHandler: function (e) {
    lineChart.scroll(e);
  },
  touchEndHandler: function (e) {
    lineChart.scrollEnd(e);
    lineChart.showToolTip(e, {
      format: function (item, category) {
        return category + ' ' + item.name + ':' + item.data
      }
    });
  },
  createSimulationData: function () {
    var categories = [];
    var data = [];
    for (var i = 0; i < 10; i++) {
      categories.push('201620162-' + (i + 1));
      data.push(Math.random() * (20 - 10) + 10);
    }
    var data = {
      categories: categories,
      data: data
    }
    console.log(data)
    return data
  },
  pieTouchHandler: function (e) {
    console.log(pieChart.getCurrentDataIndex(e));
  },
  async prepareData() {
    try {
      const data = await this.loadData()
      return data
      console.log(res);
    } catch (err) {
      console.log(err);
    }
  },
  renderChart(dataInfo) {
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    lineChart = new wxCharts({
      canvasId: 'lineCanvas',
      type: 'line',
      categories: dataInfo.stickLineData.dates,
      animation: false,
      series: [{
        name: '每次检测竹签数',
        data: dataInfo.stickLineData.stickNums,
        // format: function (val, name) {
        //   return val.toFixed(2) + '万';
        // }
      }],
      xAxis: {
        disableGrid: false
      },
      yAxis: {
        title: '每日检测竹签数量(个)',
        format: function (val) {
          return val.toFixed(2);
        },
        min: 0
      },
      width: windowWidth,
      height: 200,
      dataLabel: true,
      dataPointShape: true,
      enableScroll: true,
      extra: {
        lineStyle: 'curve'
      }
    });
    pieChart = new wxCharts({
      animation: true,
      canvasId: 'pieCanvas',
      type: 'pie',
      series: dataInfo.stickPieDataList,
      width: windowWidth,
      height: 300,
      dataLabel: true,
    });
  },
  onLoad: function (e) {
    this.loadData()
  }
});